<template>
  <div>
    <el-button type="primary" @click="dialogTableVisible = true">添加</el-button>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="添加" width="600px">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 450px; margin-left:50px;">
        <el-form-item :label="'地址'" prop="address">
          <el-input v-model="row_data.address" placeholder="地址"/>
        </el-form-item>
        <el-form-item :label="'手机号'" prop="phone">
          <el-input v-model="row_data.phone" placeholder="手机号"/>
        </el-form-item>
        <el-form-item :label="'网址'" prop="url">
          <el-input v-model="row_data.url" placeholder="网址"/>
        </el-form-item>
        <el-form-item :label="'qq'" prop="qq">
          <el-input v-model="row_data.qq" placeholder="qq"/>
        </el-form-item>
        <el-form-item :label="'微信'" prop="wechat">
          <el-input v-model="row_data.wechat" placeholder="微信"/>
        </el-form-item>
        <el-form-item :label="'二维码图片'" prop="head_img">
          <div class="upload_head_img">
            <el-upload :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="upload_picture" class="avatar-uploader" action="">
              <img v-if="row_data.qr_code" :src="row_data.qr_code" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"/>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item :label="'是否默认'" prop="status">
          <el-radio v-for="item in is_default_status" v-model="row_data.is_default" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即创建</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import { add } from '@/api/article/contactUs'
import store from '@/store'
import { client } from '@/utils/alioss'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  data() {
    return {
      select_type: [],
      select_status: [
        { value: 0, label: '禁用' },
        { value: 1, label: '正常' }
      ],
      is_default_status: [
        { value: 0, label: '否' },
        { value: 1, label: '是' }
      ],
      rules: {
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '网址不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        qq: [
          { required: true, message: 'qq不能为空', trigger: 'blur' }
        ],
        wechat: [
          { required: true, message: '微信不能为空', trigger: 'blur' }
        ],
        qr_code: [
          { required: true, message: '二维码不能为空', trigger: 'blur' }
        ],
        is_default: [
          { required: true, message: '是否默认不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      row_data: {}
    }
  },
  created: function() {
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 1MB!')
      }
      return (isJPG | isPNG) && isLt2M
    },
    upload_picture(file) {
      const that = this
      const fileName = 'user-admin_user-picture' + file.file.uid
      client().put(fileName, file.file).then((res) => {
        this.$set(that.row_data, 'qr_code', res.url)
        that.$message.success('头像地址上传成功！')
      }).catch((err) => {
        that.$message.error('头像地址上传失败，原因：' + err)
      })
    },
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = {
            'address': this.row_data.address,
            'phone': this.row_data.phone,
            'url': this.row_data.url,
            'qq': this.row_data.qq,
            'wechat': this.row_data.wechat,
            'qr_code': this.row_data.qr_code,
            'status': this.row_data.status,
            'is_default': this.row_data.is_default
          }
          add(store.getters.token, data).then(res => {
            if (res.data.status) {
              this.$message.success('添加成功！')
              this.$emit('getLists')
              this.close()
            } else {
              this.$message.error('添加失败！原因：' + res.data.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.dialogTableVisible = false
    }
  }
}
</script>

<style>
  /* 图片上传的样式 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }

  /* 头像地址(head_img)图片上传组件的尺寸 */
  .upload_head_img .avatar-uploader .el-upload{
    border-radius: 40px;
  }
  .upload_head_img .avatar-uploader-icon{
    font-size: 28px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  .upload_head_img .avatar{
    width: 80px;
    height: 80px;
  }
</style>
